package controller

import (
	"bluebell/logic"
	"bluebell/models"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
)

func PostVoteHandler(c *gin.Context) {
	// 参数校验
	p := new(models.ParamVoteData)
	if err := c.ShouldBindJSON(&p); err != nil {
		errs, ok := err.(validator.ValidationErrors) // 类型断言
		if !ok {
			ResponseError(c, CodeInvalidParams)
			return
		}
		ResponseErrorWithMsg(c, CodeInvalidParams, removeTopStruct(errs.Translate(trans)))
		return

	}
	uid, err := GetCurrentUser(c)
	if err != nil {
		ResponseError(c, CodeNeedLogIN)
		return
	}

	// 投票的业务逻辑
	err = logic.PostVote(uid, p)
	if err != nil {
		ResponseError(c, CodeServerBusy)
		zap.L().Error("logic.PostVote failed", zap.Error(err))
		return
	}

	ResponseSuccess(c, nil)
	return

}
